package org.xilin

class RoomScheduleController {
    
    def authenticateService

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list")
    }

    def list = {
        //params.max = Math.min(params.max ? params.int('max') : 10, 100)
        render(template: "list", model: [roomScheduleInstanceList: RoomSchedule.list(params), roomScheduleInstanceTotal: RoomSchedule.count(), roles:session.roles])
    }

    def create = {
        def roomScheduleInstance = new RoomSchedule()
        roomScheduleInstance.properties = params
        render(template: "create", model: [roomScheduleInstance: roomScheduleInstance])
    }

    def commonSave(params, clazz) {
        def instance

        if (params.id) {
            instance = clazz.get(params.id)
        }
        else {
            instance = clazz.newInstance();
        }

        instance.properties = params
        if (!instance.validate()) {
            flash.message = "Missing required data"
        }
        else {
            instance.save(flush:true)
            instance.refresh()
        }

        return instance
    }

    def save = {
        def roomScheduleInstance = commonSave(params, RoomSchedule)
        if (flash.message != null) {
            render(template: params.view, model: [roomScheduleInstance: roomScheduleInstance])
            return
        }
        //render(template: params.view, model: [roomScheduleInstance: roomScheduleInstance])
        redirect(action: "list")
    }

    def show = {
        def roomScheduleInstance = RoomSchedule.get(params.id)
        if (!roomScheduleInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'roomSchedule.label', default: 'Room'), params.id])}"
            redirect(action: "list")
        }
        else {
            [roomScheduleInstance: roomScheduleInstance]
        }
    }

    def edit = {
        def roomScheduleInstance = RoomSchedule.get(params.id)
        if (!roomScheduleInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'roomSchedule.label', default: 'Room'), params.id])}"
            redirect(action: "list")
        }
        else {
            render(template: "edit", model: [roomScheduleInstance: roomScheduleInstance])
        }
    }

    def update = {
        def roomScheduleInstance = RoomSchedule.get(params.id)
        if (roomScheduleInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (roomScheduleInstance.version > version) {
                    
                    roomScheduleInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'roomSchedule.label', default: 'Room')] as Object[], "Another user has updated this RoomSchedule while you were editing")
                    render(view: "edit", model: [roomScheduleInstance: roomScheduleInstance])
                    return
                }
            }
            roomScheduleInstance.properties = params
            if (!roomScheduleInstance.hasErrors() && roomScheduleInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'roomSchedule.label', default: 'Room'), roomScheduleInstance.id])}"
                redirect(action: "show", id: roomScheduleInstance.id)
            }
            else {
                render(view: "edit", model: [roomScheduleInstance: roomScheduleInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'roomSchedule.label', default: 'Room'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def roomScheduleInstance = RoomSchedule.get(params.id)
        if (roomScheduleInstance) {
            try {
                roomScheduleInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'roomSchedule.label', default: 'Room Schedule'), params.id])}"
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'roomSchedule.label', default: 'Room Schedule'), params.id])}"
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'roomSchedule.label', default: 'RoomS chedule'), params.id])}"
        }
        redirect(action: "list")
    }
}
